package com.jeeplus.modules.api.wxpay;

import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * @Date: 2018/8/10 20:59
 * @Description: test
 */
public class WxTx {

    @Autowired
    HttpServletRequest request;
    private final SortedMap<String, String> sortedMap = new TreeMap<>();

    public EnterprisesPayment addEnterprisesPayment(String openid, String sginCode,Integer amount, String appId, String mchId) {
        EnterprisesPayment enterprisesPayment = new EnterprisesPayment();
        enterprisesPayment.setMch_appid(appId);//商户号appid
        enterprisesPayment.setMchid(mchId);//商户号 
        System.out.println("nonce_str:" + sortedMap.get("nonce_str"));
        enterprisesPayment.setNonce_str(sortedMap.get("nonce_str"));//随机字符串 
        enterprisesPayment.setSign(sginCode);//签名 
        System.out.println("nonce_str:" + sortedMap.get("partner_trade_no"));
        enterprisesPayment.setPartner_trade_no(sortedMap.get("partner_trade_no"));//商户订单号 
        enterprisesPayment.setOpenid(openid);
        enterprisesPayment.setCheck_name(sortedMap.get("check_name"));
        enterprisesPayment.setAmount(amount);//金额 
        enterprisesPayment.setDesc(sortedMap.get("desc"));//描述 
        enterprisesPayment.setSpbill_create_ip(sortedMap.get("spbill_create_ip"));//ip地址 
        return enterprisesPayment;
    }

    public String getSgin(String openid, Integer amount, String info, String appId, String mchId, String mchSecret) {
        sortedMap.put("mch_appid", appId);
        sortedMap.put("mchid", mchId);
        sortedMap.put("nonce_str", WxPayUtils.getUUID());
        sortedMap.put("partner_trade_no", WxPayUtils.getOrderId());
        sortedMap.put("openid", openid);
        sortedMap.put("check_name", "NO_CHECK");
        sortedMap.put("amount", String.valueOf(amount));
        sortedMap.put("desc", info);
        //ip地址
        String spbill_create_ip = IpUtil.getIpAddr(request);
        sortedMap.put("spbill_create_ip", "123.149.215.158");
        System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        System.out.println(sortedMap.toString());
        System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        return WxPayUtils.getSignCode(sortedMap,mchSecret);
        //return createSign(sortedMap, mchSecret);
    }
}
